package main

import (
	"encoding/json"
	"io/ioutil"
	"os"
	"server/src/common"
)

const BackupConfigFileName = "./configs/gpsprocessor2d.json"

type Config struct {
	TcpPort int      //监听端口
	DBUrl   string   //Mongo数据库服务器的连接字符串
	DBName  string   //数据库名
	DBUser  string   //数据库用户名
	DBPwd   string   //数据库密码
	Debug   bool     //debug
	Log     struct { //日志输出
		Type int    //0:stdout; 1:file; 2:syslog
		Path string //当Type为1时，Path才有意义，否则忽略
	}
	ProcessorNum          int    //线程数量
	SortField             string //排序符号
	MaxBirthWarmPress0200 int    //最大GPS0200的定时器时间,默认10
}

func loadConfig(file string) (*Config, error) {
	if file == "" {
		file = os.Args[0] + ".json"
	}
	if _, err := os.Stat(file); os.IsNotExist(err) {
		file = BackupConfigFileName
	}
	buf, err := ioutil.ReadFile(file)
	if err != nil {
		return nil, err
	}

	var cfg Config
	err = json.Unmarshal(buf, &cfg)
	if cfg.ProcessorNum == 0 {
		cfg.ProcessorNum = 2
	}
	if cfg.SortField == "" {
		cfg.SortField = common.OrderAsc
	}
	if cfg.MaxBirthWarmPress0200 == 0 {
		cfg.MaxBirthWarmPress0200 = 10
	}

	return &cfg, err
}
